Imports System.Data

Partial Class Admin_Add_Freq
    Inherits System.Web.UI.Page

    Private Sub SecurePage()
        Dim sec As New Secure

        sec.ParentPage = Me
        sec.SetPageAccess(Session("UserName"))
        'update
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 3, Secure.ControlTypes.DeleteType)
        'update
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 4, Secure.ControlTypes.EditType)
        sec = Nothing
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If DataGrid.Items.Count < 1 Then
            'ViewState.Clear()

            Dim zClass As New LookUp.General_Lookup

            zClass.LookUpCode = "FREQUENCY"
            DataGrid.CurrentPageIndex = 0
            zClass.DataGridFill(DataGrid, Results_lbl)

        End If

        Dim Edit_Image As Image = CType(Me.Master.FindControl("L9"), Image)
        Edit_Image.ImageUrl = "~/images/redT.PNG"
        Dim Edit_Button As LinkButton = CType(Me.Master.FindControl("FrequencyofPayment_btn"), LinkButton)
        Edit_Button.ForeColor = Drawing.Color.Black
        Dim Edit_Panel As Panel = CType(Me.Master.FindControl("Lookup_Panel"), Panel)
        Edit_Panel.Visible = True
        SecurePage()
    End Sub

    Protected Sub Add_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Add_btn.Click
        OnMode("Add") : ViewState("mode") = "Add"
        Code_txt.Text = "" : Name_txt.Text = ""
        Code_txt.Focus()
    End Sub

    Private Sub OnMode(ByVal mode As String)

        If mode = "Edit" Then
            Code_txt.Enabled = True
            Name_txt.Enabled = True
        End If
        DataGrid.Enabled = False
        Add_btn.Enabled = False
        AddEdit_Panel.Visible = True

    End Sub

    Private Sub NotOnMode()
        Code_txt.Enabled = True
        Name_txt.Enabled = True
        DataGrid.Enabled = True
        Add_btn.Enabled = True
        AddEdit_Panel.Visible = False

    End Sub

    Protected Sub DataGrid_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.DeleteCommand
        Dim zClass As New LookUp.General_Lookup
        Dim dm As New DAOMain.CommonDAO
        zClass.LookUpCode = "FREQUENCY"

        'paki update sa lookup.vb, all tables which use frequency
        If zClass.OKtoDELETE(Trim(e.Item.Cells(0).Text)) = False Then Exit Sub

        zClass.ExecQuery("DELETE FROM frequency_mstr WHERE frequency_cd=" & dm.Q(Trim(e.Item.Cells(0).Text)))

        DataGrid.CurrentPageIndex = 0
        zClass.DataGridFill(DataGrid, Results_lbl)

    End Sub

    Protected Sub DataGrid_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.EditCommand
        ViewState("mode") = "Edit"

        Code_txt.Text = e.Item.Cells(2).Text
        Name_txt.Text = e.Item.Cells(1).Text
        ViewState("freq_cd") = Trim(e.Item.Cells(0).Text)
        OnMode("Edit")
        Name_txt.Focus()
    End Sub

    Protected Sub Cancel_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cancel_btn.Click
        NotOnMode()
        ViewState.Remove("mode")
        ViewState.Remove("freq_cd")
    End Sub

    Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        Dim zClass As New LookUp.General_Lookup
        Dim dm As New DAOMain.CommonDAO

        'Validation
        If Val(Me.Code_txt.Text) < 0 Or Val(Code_txt.Text) > 12 Then
            Dim polyfunc As New PolyFunc.ASPFunc

            polyfunc.MessageAlert(Me, "Please enter a number between 0 and 12 for the frequency count!")
            polyfunc = Nothing

            Exit Sub
        End If

        If ViewState("mode") = "Add" Then
            zClass.LookUpCode = "FREQUENCY"
            zClass.ExecQuery("INSERT INTO frequency_mstr(frequency_cd, frequency_count, frequency_desc, " & _
                "frequency_crea_by, frequency_crea_dt) VALUES('" & _
                zClass.MaxCode & "'," & UCase(Trim(Code_txt.Text)) & "," & _
                UCase(dm.Q(Trim(Name_txt.Text))) & ",'" & UCase(Session("UserName")) & "','" & Now & "')")

        ElseIf ViewState("mode") = "Edit" Then
            zClass.ExecQuery("UPDATE frequency_mstr SET " & _
                "frequency_desc=" & UCase(dm.Q(Trim(Name_txt.Text))) & ", " & _
                "frequency_count=" & UCase(Trim(Code_txt.Text)) & ", " & _
                "frequency_last_upd_by='" & UCase(Session("UserName")) & "', " & _
                "frequency_last_upd_dt='" & Now & "' " & _
                "WHERE frequency_cd='" & ViewState("freq_cd") & "'")

        End If

        zClass.LookUpCode = "FREQUENCY"
        zClass.DataGridFill(DataGrid, Results_lbl)
        NotOnMode()
        ViewState.Remove("mode")
        ViewState.Remove("freq_cd")
    End Sub

    Protected Sub DataGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid.PageIndexChanged
        DataGrid.CurrentPageIndex = e.NewPageIndex
        Dim zClass As New LookUp.General_Lookup
        zClass.LookUpCode = "FREQUENCY"
        zClass.DataGridFill(DataGrid, Results_lbl)
    End Sub
End Class